import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import io.github.biezhi.elves.common.CookieConstants;
import io.github.biezhi.elves.model.StockStructureInfo;
import io.github.biezhi.elves.ok.http.FastHttpClient;
import io.github.biezhi.elves.utils.StringUtils;

public class testStockStucture {
	static String unique = "";
	public static void main(String[] args) {
		String url="http://www.qichacha.com/cms_guquanmap?keyNo=d3ac21e81f8aac1ba9e2f37ce3db3f84";
		try {
			io.github.biezhi.elves.ok.http.Response response = FastHttpClient.get().addHeader("cookie", CookieConstants.QYY_CCA_COOKIE).url(url).build().execute();
			String jsonStr = response.string();
			System.out.println(jsonStr);
			saveStockStructureData(jsonStr, unique);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 解析json数据并保存股权结构数据入库
	 * @param jsonStr
	 */
	private static void saveStockStructureData(String jsonStr,String unique){
		String resultJson = "";
		boolean isChildren = false;
		try {
			JSONObject json = JSONObject.parseObject(jsonStr);
			if(json.containsKey("Result")){
				resultJson = json.getString("Result");
			}else{
				resultJson = json.getString("Children");
			}
			
			JSONObject resultJsonObject = JSONObject.parseObject(resultJson);
			StockStructureInfo stockStructureInfo = JSON.parseObject(resultJson, StockStructureInfo.class);
			
			if(!StringUtils.isNullOrEmpty(unique)){
				stockStructureInfo.setUniqueId(unique);
			}
			if(!isChildren){
				stockStructureInfo.setParentKeyNo("0");
			}else{
				stockStructureInfo.setParentKeyNo(resultJsonObject.getString("ParentKeyNo"));
			}
			System.out.println("非循环体{{}}==>json转换对象数据{{}}==>" + stockStructureInfo);
			if(resultJsonObject.containsKey("Children") && resultJsonObject.get("Children")!=null){
				JSONArray array = resultJsonObject.getJSONArray("Children");
				for (int i = 0; i < array.size(); i++) {
					JSONObject arrJsonObject = array.getJSONObject(i);
					String arrJsonStr = arrJsonObject.toJSONString();
					stockStructureInfo = JSON.parseObject(arrJsonStr, StockStructureInfo.class);
					if(!StringUtils.isNullOrEmpty(unique)){
						stockStructureInfo.setUniqueId(unique);
					}
					System.out.println("循环体{{}}==>json转换对象数据{{}}==>" + stockStructureInfo);
				    if(arrJsonObject.containsKey("Children") && (arrJsonObject.get("Children")!=null || !"null".equals(arrJsonObject.get("Children").toString()))){
				    	//解析子json数据
				    	saveStockStructureChildrenData(arrJsonObject.get("Children").toString(),unique);
				    }
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 解析子json数据
	 * @param jsonStr
	 */
	private static void saveStockStructureChildrenData(String jsonStr,String unique){
		JSONArray array = JSONArray.parseArray(jsonStr);
		if(array!=null){
			for (int i = 0; i < array.size(); i++) {
				JSONObject arrJsonObject = array.getJSONObject(i);
				String arrJsonStr = arrJsonObject.toJSONString();
				StockStructureInfo stockStructureInfo = JSON.parseObject(arrJsonStr, StockStructureInfo.class);
				if(!StringUtils.isNullOrEmpty(unique)){
					stockStructureInfo.setUniqueId(unique);
				}
				System.out.println("子循环体{{}}==>json转换对象数据{{}}==>" + stockStructureInfo);
			}
		}
	}
}